import { defineStore } from 'pinia'

import { reqC1, reqC2, reqC3 } from '@/api/product/attr'
import type { CategoryResponseData } from '@/api/product/attr/type'
import type { CategoryState } from '@/store/types/type'

//定义分类的Store
export const useCategoryStore = defineStore('Category', {
    state: (): CategoryState => ({
        c1Id: '',
        c1Arr: [],
        c2Arr: [],
        c2Id: '',
        c3Arr: [],
        c3Id: '',
    }),
    actions: {
        //获取一级分类
        async getCategory1() {
            let result: CategoryResponseData = await reqC1()
            if (result.code === 200) {
                this.c1Arr = result.data
            }
        },
        //获取二级分类
        async getCategory2() {
            let result: CategoryResponseData = await reqC2(this.c1Id)
            if (result.code === 200) {
                this.c2Arr = result.data
            }
        },
        //获取三级分类
        async getCategory3() {
            let result: CategoryResponseData = await reqC3(this.c2Id)
            if (result.code === 200) {
                this.c3Arr = result.data
            }
        },
    },
})
